package com.yaorange.jxc.supplier.service.impl;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.yaorange.jxc.supplier.entity.Supplier;
import com.yaorange.jxc.supplier.entity.SupplierPageRequestResult;
import com.yaorange.jxc.supplier.mapper.SupplierMapper;
import com.yaorange.jxc.supplier.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @company: yaorange
 * @author: wu YanZu
 * @version: 1.0
 * @create: 2022-04-26 18:36:41
 */
@Slf4j
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Transactional
    @Override
    public Map<String, Object> findSupplisrList(SupplierPageRequestResult requestResult) {
        Integer pageNum = requestResult.getPage();
        Integer pageSize = requestResult.getRows();
        String supplierName = requestResult.getSupplierName();

        QueryWrapper<Supplier> supplierQueryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(supplierName)){
            supplierQueryWrapper.like("supplier_name",supplierName);
        }
        Page<Supplier> supplierPage = supplierMapper.selectPage(new Page<>(pageNum, pageSize), supplierQueryWrapper);
        List<Supplier> records = supplierPage.getRecords();
        Integer integer = supplierMapper.selectCount(supplierQueryWrapper);

        HashMap<String, Object> result = new HashMap<>();
        result.put("tatol",integer);
        result.put("rows",records);

        return result;
    }

    @Override
    public Result<?> saveById(Integer supplierId, Supplier supplier) {
        Supplier supplierExist = supplierMapper.selectById(supplierId);
        supplier.setSupplierId(supplierId);
        int i = 0;
        if(supplierExist.getSupplierName() != null){
            i = supplierMapper.updateById(supplier);
        }
        i = supplierMapper.insert(supplier);
        if(i == 0){
            return Result.error(400,"请求失败");
        }
        return Result.OK("请求成功",i);
    }

    @Override
    public Integer deleteById(List<Integer> ids) {
        int count = 0;
        for (Integer id:ids
             ) {
            int i = supplierMapper.deleteById(id);
            count += i;
        }
        return count;
    }






    @Override
    public List<Supplier> getList() {
        return supplierMapper.selectList(new QueryWrapper<>());
    }
}
